<?php
namespace app\admin\controller;

use think\facade\View;
use app\Request;
use app\repositories\contracts\RootGroupInterface;
use app\repositories\contracts\MenuInterface;

class RootGroup extends ViewBaseController
{
    public function index()
    {
        //halt(md5('vee7z'.'yhw1230'));
        return View::fetch('index');
    }

    public function getRootGroupListJson(Request $request,RootGroupInterface $group)
    {
        $page = $request->param('page');
        $limit = $request->param('limit');
        return layuiJson(0,'用户组列表获取成功',$group->getRootGroupListPage($page,$limit,'id','asc'),$group->getRootGroupListCount());
    }

    /***
     * @param RootGroupInterface $group
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:21
     * @Description 删除用户组接口
     */
    public function del(Request $request,RootGroupInterface $group)
    {
        $idArray = $request->param('idArray');
        $bool = $group->delRootGroupList($idArray);
        if(!$bool)
        {
            $result = layuiJson(1,'删除失败');
            return $result;
        }
        return layuiJson(0,'删除成功',$idArray);
    }

    /***
     * @param Request $request
     * @param RootGroupInterface $group
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:40
     * @Description 修改指定字段接口
     */
    public function editList(Request $request,RootGroupInterface $group)
    {
        $editArray = $request->param();

        $result = $group->editRootGroupList($editArray);
        if(!$result)
        {
            $result = layuiJson(1,'修改失败');
            return $result;
        }
        return layuiJson(0,'修改成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 13:46
     * @Description 添加用户组
     */
    public function add(Request $request,RootGroupInterface $group)
    {
        if(!$request->isPost()){
            $allRootGroupList = $group->getRootGroupList();
            return View::fetch('',['allRootGroupList'=>$allRootGroupList]);
        }

        $addArray = $request->param();
        $result = $group->addRootGroupList($addArray);
        if(!$result){
            return layuiJson(1,'添加失败',$result);
        }

        return layuiJson(0,'添加成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 10:20
     * @Description 会员组权限管理页面
     */
    public function auth(Request $request,RootGroupInterface $group,MenuInterface $menu)
    {
        $id = $request->param('id');
        if(!$id){
            return redirect('/admin/root_group/index');
        }
        if(!$request->isPost()){
            $RootGroupData = $group->getRootGroupData($id);
            $MenuListData = $menu->getListForAuth($id);
            return View::fetch('',['RootGroupData'=>$RootGroupData,'MenuListData'=>json_encode($MenuListData)]);
        }

        $addArray = $request->param();
        $result = $group->addRootGroupList($addArray);
        if(!$result){
            return layuiJson(1,'添加失败',$result);
        }

        return layuiJson(0,'添加成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/17
     * @Time 10:02
     * @Description 无访问权限页面
     */
    public function NoPromise($errorMsg="非法访问")
    {

        return View::fetch('nopromise',['errorMsg'=>$errorMsg]);
    }
}
